﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Linq;

namespace Game
{
    public class Character : VisibleGameEntity
    {
        public override bool Init(ContentManager Content, int n, string strResource,int nframe, int dframe)
        {
            _nSprite = 1;
            _Sprite = new MySprite[_nSprite];
            Texture2D texture2D;
            //Texture2D[] texture2D;
           
            //texture2D = new Texture2D[n];
            //for (int i = 0; i < n; i++)
            //    texture2D[i] = Content.Load<Texture2D>(@"OnePiece/" + strResource+i.ToString("00"));
            texture2D = Content.Load<Texture2D>(@"OnePiece/" + strResource);

           // int nframe = 10;
           // int dframe = 15;
            _Sprite[0] = new MySprite(texture2D, nframe, dframe, 100.0f, 100.0f);

            return true;
        }

        public override VisibleGameEntity Clone()
        {
            VisibleGameEntity newObject = new Character();
            newObject._nSprite = this._nSprite;
            newObject._Sprite = new MySprite[this._nSprite];
            for (int i = 0; i < this._nSprite; i++)
            {
                newObject._Sprite[i] = new MySprite(this._Sprite[i].texture2d, this._Sprite[i].nFrame, this._Sprite[i].dFrame, X, Y); 
            }
            newObject.X = this.X;
            newObject.Y = this.Y;
            return newObject;
        }

        public delegate void KeyPressedHandler(object sender,
          Keys key);
        public event KeyPressedHandler KeyPressed;


        Keys[] prevKeys = new Keys[0];
        public Keys GetNewKeyPressed(Keys[] prevKeys, Keys[] keys)
        {
            throw new NotSupportedException("Coding by yourself");
        }

        public void updatekey(GameTime gameTime, int select)
        {
            // Get state of the keyboard
            KeyboardState state = Keyboard.GetState();

            // Do events
            if (this.KeyPressed != null)
                this.KeyPressed(this, this.GetNewKeyPressed(
                    prevKeys, state.GetPressedKeys()));
            prevKeys = state.GetPressedKeys();

            // Moving with iteraction
            if (state.IsKeyDown(Keys.Left))
            {
                if (this._Sprite[select].x > 0)
                    this._Sprite[select].x -= 5;
                else this._Sprite[select].x = this._Sprite[select].x;
            }
            else if (state.IsKeyDown(Keys.Right))
            {
                if ((this._Sprite[select].x + 50) < 768)
                    this._Sprite[select].x += 5;
                else this._Sprite[select].x = this._Sprite[select].x;
            }
            else if (state.IsKeyDown(Keys.Up))
            {
                if (this._Sprite[select].y > 0)
                    this._Sprite[select].y -= 5;
                else this._Sprite[select].y = this._Sprite[select].y;
            }
            else if (state.IsKeyDown(Keys.Down))
            {
                if ((this._Sprite[select].y + 96) < 764)
                    this._Sprite[select].y += 5;
                else this._Sprite[select].y = this._Sprite[select].y;
            }
            //for (int i = 0; i < this._nSprite; i++)
            //{
            //    this._Sprite[i].itexture2d = (this._Sprite[i].itexture2d + 1) % this._Sprite[i].ntexture2d;
            //}
            //  _itexture2d = (_itexture2d + 1) % ntexture2d;
            //  this._Sprite[select].itexture2d = (this._Sprite[select].itexture2d + 1) % this._Sprite[select].ntexture2d;

        }

        public void updatekey(GameTime gameTime, int select, int desX, int desY)
        {
            // Get state of the keyboard
            KeyboardState state = Keyboard.GetState();

            // Do events
            if (this.KeyPressed != null)
                this.KeyPressed(this, this.GetNewKeyPressed(
                    prevKeys, state.GetPressedKeys()));
            prevKeys = state.GetPressedKeys();

            // Moving with iteraction
            if (state.IsKeyDown(Keys.Left))
            {

                this._Sprite[select].x -= 5;
                Game1.desX -= 5;
            }
            else if (state.IsKeyDown(Keys.Right))
            {

                this._Sprite[select].x += 5;
                Game1.desX += 5;
            }
            else if (state.IsKeyDown(Keys.Up))
            {

                this._Sprite[select].y -= 5;
                Game1.desY -= 5;
            }
            else if (state.IsKeyDown(Keys.Down))
            {

                this._Sprite[select].y += 5;
                Game1.desY += 5;
            }
        }

        public bool updateMouse(GameTime gameTime, int select, int desX, int desY)
        {
            if ((this._Sprite[select].x + 5) < (desX - 64))
                this._Sprite[select].x += 5;
            else if ((this._Sprite[select].x - 5) > (desX - 64))
                this._Sprite[select].x -= 5;
            else this._Sprite[select].x = desX - 64;

            if ((this._Sprite[select].y + 5) < (desY - 64))
                this._Sprite[select].y += 5;
            else if ((this._Sprite[select].y - 5) > (desY - 64))
                this._Sprite[select].y -= 5;
            else this._Sprite[select].y = desY - 64;

            if (this._Sprite[select].x == desX - 64 && this._Sprite[select].y == desY - 64)
                return true;
            else return false;
        }

        public  void Update(GameTime gameTime,int select, string stateCharacter)
        {
            if (stateCharacter == "stand")//0-3
            {
                if (Game1.time == 10)
                {
                    if (this._Sprite[select].iFrame < 0 || this._Sprite[select].iFrame > 2)//the hien toi iframe 11
                        this._Sprite[select].iFrame = 0;
                    else this._Sprite[select].iFrame++;
                    Game1.time = 0;
                }
                else Game1.time++;
            }
            else if (stateCharacter == "run")//4-11
            {
                if (Game1.time == 10)
                {
                    if (this._Sprite[select].iFrame < 4 || this._Sprite[select].iFrame > 10)//the hien toi iframe 11
                        this._Sprite[select].iFrame = 4;
                    else this._Sprite[select].iFrame++;
                    Game1.time = 0;
                }
                else Game1.time++;
            }

        }

    }
}
